﻿#region (C) Header Region @ 2012
//====================================================
// 文件: ConfigSectionListenterService.cs
// 描述: 
// ******************版本1 ******************
// 人员: 蔡金保    时间 : 2012/12/15 15:15:54
// 注释: 该服务监听为动态配置ConfigSection服务，实现热插操作
//====================================================
#endregion
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Text;

namespace Antu.Core.Services
{
    /// <summary>
    /// ConfigurationSection监听服务
    /// </summary>
    public class ConfigSectionListenterService : AbstractService
    {
        internal ConfigSectionListenterService()
        {
        }
        Dictionary<string, string> fileNameCache = new Dictionary<string, string>();
        Dictionary<string, object> sectionCache = new Dictionary<string, object>();
        List<string> pathCache = new List<string>();

        #region 监视配置文件
        protected void AddWatch(string sectionName, string fileFullName)
        {
            string filePath = Path.GetDirectoryName(fileFullName);
            if (!pathCache.Contains(filePath))
            {
                pathCache.Add(filePath);
                AddWatch(filePath);
            }
            string filename = Path.GetFileName(fileFullName);
            if (!fileNameCache.ContainsKey(filename))
            {
                fileNameCache.Add(filename, sectionName);
            }
        }
        private void AddWatch(string watchPath)
        {
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = watchPath;
            watcher.Filter = "*.config";
            watcher.NotifyFilter = NotifyFilters.Size;
            watcher.Changed += new FileSystemEventHandler(watcher_Changed);
            watcher.EnableRaisingEvents = true;
        }
        private void watcher_Changed(object sender, FileSystemEventArgs e)
        {
            if (fileNameCache.ContainsKey(e.Name))
            {
                string sectionName = fileNameCache[e.Name];
                sectionCache[sectionName] = ConfigurationManager.GetSection(sectionName);
                if (sectionName == "logProvider")
                {
                    Antu.Core.Logger.LogManager.Manager.InitLogManage();
                }
            }
        }
        #endregion

        /// <summary>
        /// 添加需要监听的配置文件
        /// </summary>
        private void AddConfigSection()
        {
            AddWatch("logProvider", System.Web.HttpContext.Current.Server.MapPath("~/ConfigSettings/configSections/logProvider.config"));
        }

        private void ClearSection()
        {
            fileNameCache.Clear();
            sectionCache.Clear();
            pathCache.Clear();
        }

        #region 获取Section
        public object GetSection(string sectionName)
        {
            if (!sectionCache.ContainsKey(sectionName))
            {
                sectionCache.Add(sectionName, ConfigurationManager.GetSection(sectionName));
            }
            return sectionCache[sectionName];
        }
        public T GetSection<T>(string sectionName) where T : ConfigurationSection
        {
            return (T)GetSection(sectionName);
        }
        #endregion

        /// <summary>
        /// 初始化服务(配置需要监听的配置文件)
        /// </summary>
        public override void InitializeService()
        {
            AddConfigSection();
            base.InitializeService();
        }
        /// <summary>
        /// 卸载服务
        /// </summary>
        public override void UnloadService()
        {
            ClearSection();
            base.UnloadService();
        }
    }
}
